package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-23
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    //注入vod
    @Autowired
private VodClient vodClient;


    @Override
    public void removeVideoByCourseId(String course_id) {


        //根据courseId 在Video表中查出 所有 跟这个courseId有关的 vedio实体。
        //遍历实体得到每一个sourceId并且将 他放入一个list里面
        QueryWrapper<EduVideo> queryWrapper  = new QueryWrapper<>();

        queryWrapper.eq("course_id",course_id);
        queryWrapper.select("video_source_id"); //只需要查询某个字段的时候
        List<EduVideo> eduVideos = baseMapper.selectList(queryWrapper);

        List<String> list = new ArrayList<>();

        for (int i = 0; i < eduVideos.size(); i++) {
            EduVideo eduVideo = eduVideos.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId))
            {
                list.add(videoSourceId);
            }
        }

        //根据是否有值进行删除判断

        //多个视频id删除多个视频
        if(list.size()>0){
            vodClient.deleteBatch(list);
        }


        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",course_id);
        baseMapper.delete(wrapper);
    }
}
